﻿using System;
using System.Diagnostics;
using System.Windows.Forms;
using Microsoft.Win32;

namespace SharePointUtilitiesForDevelopers.Actions.Forms
{
    /// <summary>
    /// A form for displaying generated Urls.
    /// </summary>
    public partial class UrlForm : Form
    {
        private string _browser;
        private bool _showForm = true;

        /// <summary>
        /// Initializes a new instance of the <see cref="UrlForm"/> class.
        /// </summary>
        public UrlForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the URL.
        /// </summary>
        /// <value>The URL.</value>
        public string Url
        {
            get
            {
                return urlTextBox.Text;
            }
            set
            {
                urlTextBox.Text = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this form is shown or just opens the browser.
        /// </summary>
        /// <value><c>true</c> if show form; otherwise, <c>false</c>.</value>
        public bool ShowForm
        {
            get { return _showForm; }
            set { _showForm = value; }
        }

        /// <summary>
        /// Gets or sets the URL description.
        /// </summary>
        /// <value>The URL description.</value>
        public string UrlDescription
        {
            get
            {
                return descriptionLabel.Text;
            }
            set
            {
                descriptionLabel.Text = value;
            }
        }

        /// <summary>
        /// Handles the LinkClicked event of the openLink control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.LinkLabelLinkClickedEventArgs"/> instance containing the event data.</param>
        private void openLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenUrl(urlTextBox.Text);
        }

        /// <summary>
        /// Opens the URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        private void OpenUrl(string url)
        {
            var browser = new Process {StartInfo = {FileName = GetDefaultBrowser(), Arguments = url}};
            browser.Start();    
        }

        /// <summary>
        /// Gets the default browser.
        /// </summary>
        /// <returns></returns>
        private string GetDefaultBrowser()
        {
            if (_browser == null)
            {
                var key = default(RegistryKey);
                try
                {
                    key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);

                    if (key != null)
                    {
                        //trim off quotes
                        var browser = key.GetValue(null).ToString().ToLower().Replace("\"", "");
                        if (!browser.EndsWith("exe"))
                        {
                            //get rid of everything after the ".exe"
                            _browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
                        }
                    }
                }
                finally
                {
                    if (key != null)
                    {
                        key.Close();
                    }
                }
            }

            return _browser;
        }

        /// <summary>
        /// Handles the Click event of the copyClipboardButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void copyClipboardButton_Click(object sender, EventArgs e)
        {
            Clipboard.SetDataObject(urlTextBox.Text);
        }

        /// <summary>
        /// Handles the Click event of the closeButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Handles the Load event of the UrlForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void UrlForm_Load(object sender, EventArgs e)
        {
            if(!ShowForm)
            {
                OpenUrl(Url);
                Close();
            }
        }

    }
}